home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Gamma Fade 1.1.2 / Gamma Utils Lib v1.1.2 / gamma.c < prev    next >
Text File  |  1993-11-09  |  7KB  |  213 lines

  1. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  2. //   Last updated 11/9/93, MJS
  3.  
  4. // * ****************************************************************************** *
  5. //
  6. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  7. //        new functionality into the library or make an A4-based library. 
  8. //    See the header file "gamma.h" for much more information. -- MJS
  9. //
  10. // * ****************************************************************************** *
  11.  
  12. #include <GestaltEqu.h>
  13. #include <Quickdraw.h>
  14. #include <Video.h>
  15. #include "gamma.h"
  16.  
  17. long            gammaUtilsInstalled;
  18. globalGammasHdl    gammaTables;
  19.  
  20. // * ****************************************************************************** *
  21. // * ****************************************************************************** *
  22.  
  23. Boolean IsGammaAvailable() {
  24.     GDHandle theGDevice;
  25.  
  26.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  27.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  28.     
  29.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice))
  30.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  31.                 TestDeviceAttribute(theGDevice, noDriver)) return(0);
  32.  
  33.     return(-1);
  34.     }
  35.  
  36. // * ****************************************************************************** *
  37. // * ****************************************************************************** *
  38.  
  39. Boolean IsOneGammaAvailable(GDHandle theGDevice) {
  40.     
  41.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  42.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  43.     
  44.     if (TestDeviceAttribute(theGDevice, screenDevice) && 
  45.             TestDeviceAttribute(theGDevice, noDriver)) return(0);
  46.     
  47.     return(-1);
  48.     }
  49.  
  50. // * ****************************************************************************** *
  51. // * ****************************************************************************** *
  52.  
  53. OSErr SetupGammaTools() {
  54.     short errorCold=0;
  55.     globalGammasHdl tempHdl;
  56.     GammaTblPtr    masterGTable;
  57.     GDHandle theGDevice;
  58.  
  59.     if (gammaUtilsInstalled == kGammaUtilsSig) return(-1);
  60.     
  61.     gammaTables = 0;
  62.     gammaUtilsInstalled = kGammaUtilsSig;
  63.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  64.         if (errorCold = GetDevGammaTable(theGDevice, &masterGTable)) return(errorCold);
  65.         
  66.         tempHdl = (globalGammasHdl) NewHandle(sizeof(globalGammas));
  67.         if (tempHdl == 0) return(errorCold = MemError());
  68.         
  69.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  70.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  71.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  72.         (*tempHdl)->theGDevice = theGDevice;
  73.         
  74.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  75.         if ((*tempHdl)->saved == 0) return(errorCold = MemError());
  76.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  77.         if ((*tempHdl)->hacked == 0) return(errorCold = MemError());
  78.     
  79.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  80.         
  81.         (*tempHdl)->next = gammaTables;
  82.         gammaTables = tempHdl;
  83.         }
  84.  
  85.     return(0);
  86.     }
  87.  
  88. // * ****************************************************************************** *
  89. // * ****************************************************************************** *
  90.  
  91. OSErr DoGammaFade(short percent) {
  92.     short errorCold=0;
  93.     register long size, i, theNum;
  94.     globalGammasHdl tempHdl;
  95.     unsigned char *dataPtr;
  96.  
  97.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  98.     
  99.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  100.     
  101.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  102.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  103.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  104.         
  105.         for(i=0; i < size; i++) {
  106.             theNum = dataPtr[i];
  107.             theNum = (theNum * percent) / 100;
  108.             dataPtr[i] = theNum;
  109.             }
  110.         
  111.         if (errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  112.             return(errorCold);
  113.         }
  114.         
  115.     return(0);
  116.     }
  117.  
  118. // * ****************************************************************************** *
  119. // * ****************************************************************************** *
  120.  
  121. OSErr DoOneGammaFade(GDHandle theGDevice, short percent) {
  122.     short errorCold=0;
  123.     register long size, i, theNum;
  124.     globalGammasHdl tempHdl;
  125.     unsigned char *dataPtr;
  126.  
  127.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  128.     for(tempHdl = gammaTables; tempHdl && (theGDevice != (*tempHdl)->theGDevice);
  129.             tempHdl = (*tempHdl)->next);
  130.         
  131.     BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  132.     dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  133.     size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  134.     
  135.     for(i=0; i < size; i++) {
  136.         theNum = dataPtr[i];
  137.         theNum = (theNum * percent) / 100;
  138.         dataPtr[i] = theNum;
  139.         }
  140.     
  141.     errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked);
  142.     
  143.     return(errorCold);
  144.     }
  145.  
  146. // * ****************************************************************************** *
  147. // * ****************************************************************************** *
  148.  
  149. OSErr DisposeGammaTools() {
  150.     globalGammasHdl tempHdl, nextHdl;
  151.  
  152.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  153.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  154.         nextHdl = (*tempHdl)->next;
  155.         DisposeHandle((Handle) (*tempHdl)->saved);
  156.         DisposeHandle((Handle) (*tempHdl)->hacked);
  157.         DisposeHandle((Handle) tempHdl);
  158.         }
  159.         
  160.     gammaUtilsInstalled = 0;
  161.     return(0);
  162.     }
  163.  
  164. // * ****************************************************************************** *
  165. // * ****************************************************************************** *
  166.  
  167. OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  168.     short errorCold=0;
  169.     CntrlParam  *myCPB;
  170.  
  171.     ((long *) theTable)[0] = 0;
  172.  
  173.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  174.             
  175.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  176.     myCPB->csCode = cscGetGamma;
  177.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  178.     *(GammaTblPtr **) myCPB->csParam = theTable;
  179.     errorCold = PBStatus((ParmBlkPtr) myCPB, 0);
  180.  
  181.     DisposePtr((Ptr) myCPB);
  182.     return(errorCold);
  183.     }
  184.  
  185. // * ****************************************************************************** *
  186. // * ****************************************************************************** *
  187.  
  188. OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  189.     CntrlParam *myCPB;
  190.     short errorCold=0;
  191.     CTabHandle cTab;
  192.     GDHandle saveGDevice;
  193.  
  194.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  195.  
  196.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  197.     myCPB->csCode = cscSetGamma;
  198.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  199.     *(GammaTblPtr **) myCPB->csParam = theTable;
  200.     errorCold = PBControl((ParmBlkPtr) myCPB, 0);
  201.  
  202.     if (errorCold == 0) {
  203.         saveGDevice = GetGDevice();
  204.         SetGDevice(theGDevice);
  205.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  206.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  207.         SetGDevice(saveGDevice);
  208.         }
  209.  
  210.     DisposePtr((Ptr) myCPB);
  211.     return (errorCold);
  212.     }
  213.